//
//  XCInTheRankViewController.m
//  SoundOfNature
//
//  Created by 梁新昌 on 16/3/15.
//  Copyright © 2016年 梁新昌. All rights reserved.
//

#import "XCInTheRankViewController.h"
#import "UIImageView+WebCache.h"
#import "XCInTheRankSongsModel.h"
#import "XCShareMusicTableViewCell.h"
#import "XCAFNetTool.h"
#import "XCUrlManger.h"
#import "XCInTheRankSongsModel.h"
#import "OnlineAVplay.h"
#import "XCPlayViewController.h"
@interface XCInTheRankViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) NSString *picUrl;

@property (nonatomic, retain) UIImageView *inTheRankImageView;

@property (nonatomic, retain) NSNumber *ranklist_id;

@property (nonatomic, copy) NSString *navigationName;

@property (nonatomic, retain) NSMutableArray *mutableArraySongName;

@property (nonatomic, retain) NSMutableArray *mutableArraySingerNAme;

@property (nonatomic, retain) NSMutableArray *nameArray;

@property (nonatomic, retain) NSMutableArray *mutableHeadIVPicurl;


@end

@implementation XCInTheRankViewController

- (void)dealloc {
    
    _tableView.delegate = nil;
    _tableView.dataSource = nil;
    
    [_dataSource release];
    [_tableView release];
    [_picUrl release];
    [_inTheRankImageView release];
    [_ranklist_id release];
    [_navigationName release];
    [_mutableArraySingerNAme release];
    [_nameArray release];
    [_mutableArraySongName release];
    [_mutableHeadIVPicurl release];
    
    [super dealloc];
}

- (instancetype)initWithRanklist_id:(NSNumber *)ranklist_id WithName:(NSString *)name
{
    self = [super init];
    if (self) {
        self.ranklist_id = ranklist_id;
        self.navigationName = name;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.navigationName;
    self.dataSource = [[NSMutableArray alloc] initWithCapacity:0];
    [self inTheRankNetRequest];
    [self inTheRankTableViewBuild];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"Image-leftBarButton"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonItemAction)];
}
- (void)inTheRankTableViewBuild {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, wide, high - 70) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, wide, high / 4)];
    self.inTheRankImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, -150, wide, high / 4 + 100)];
    [view addSubview:self.inTheRankImageView];
    
    self.tableView.tableHeaderView = view;
    
    [self.tableView registerClass:[XCShareMusicTableViewCell class] forCellReuseIdentifier:@"XCShareMusicTableViewCell"];
    
    [self.view addSubview:self.tableView];
    
    [view release];
    [_tableView release];
    [_inTheRankImageView release];
}

- (void)inTheRankNetRequest {
    [XCAFNetTool GETNetWithUrl:[XCUrlManger InTheRankNetRequest:self.ranklist_id] body:nil headerFile:nil response:XCJSON success:^(id result) {
        for (NSDictionary *dic in result[@"songs"]) {
            
            XCInTheRankSongsModel *inTheRankSongsModel = [[XCInTheRankSongsModel alloc] initWithDataSource:dic];
            if (inTheRankSongsModel.auditionList.count != 0) {
                [self.dataSource addObject:inTheRankSongsModel];
            }
            
            [inTheRankSongsModel release];
        }
        
        XCInTheRankSongsModel *Model = self.dataSource[0];
        [self.inTheRankImageView sd_setImageWithURL:[NSURL URLWithString:Model.picUrl]placeholderImage:[UIImage imageNamed:@"Image-p"]];
        [self.tableView reloadData];
        
        self.mutableArraySongName = [NSMutableArray arrayWithCapacity:0];
        self.mutableHeadIVPicurl = [NSMutableArray arrayWithCapacity:0];
        self.mutableArraySingerNAme = [NSMutableArray arrayWithCapacity:0];
        self.nameArray = [NSMutableArray arrayWithCapacity:0];
        
        for (XCInTheRankSongsModel *Model in self.dataSource) {
            [self.mutableArraySongName addObject:[Model.auditionList[0] objectForKey:@"url"]];
            [self.mutableArraySingerNAme addObject:Model.singerName];
            [self.mutableHeadIVPicurl addObject:Model.picUrl];
            [self.nameArray addObject:Model.name];
        }
        
        
    } failure:^(NSError *error) {
        NSLog(@"网络错误");
        
    }];
}
#pragma mark - tableView 的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XCShareMusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XCShareMusicTableViewCell"];
    XCInTheRankSongsModel *inTheRankSongsModel = self.dataSource[indexPath.row];
    cell.inTheRankSongsModel = inTheRankSongsModel;
    cell.numberLable.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return wide / 5;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat scale= 1 - (scrollView.contentOffset.y/(self.inTheRankImageView.frame.size.height + 100) / 2);
    scale = (scale >= 1) ? scale : 1;
    self.inTheRankImageView.transform = CGAffineTransformMakeScale(scale, scale);
    
//    CGFloat offset=scrollView.contentOffset.y;
//    
//    if (offset<0) {
//        self.navigationController.navigationBar.barTintColor = [self.color colorWithAlphaComponent:0];
//    }else {
//        CGFloat alpha=1-((64-offset)/64);
//        self.navigationController.navigationBar.barTintColor=[self.color colorWithAlphaComponent:alpha];
//    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld", (long)indexPath.row);
    
    [XCPlayViewController sharePlayViewController].selectIndex = indexPath.row;
    
    [XCPlayViewController sharePlayViewController].mutableArraySongName = self.mutableArraySongName;
    
    [XCPlayViewController sharePlayViewController].mutableHeadIVPicurl = self.mutableHeadIVPicurl;
    
    [XCPlayViewController sharePlayViewController].nameArray = self.nameArray;
    [XCPlayViewController sharePlayViewController].muatbleArraySingerName = self.mutableArraySingerNAme;
    
    XCInTheRankSongsModel *inTheRankSongsModel = self.dataSource[indexPath.row];
    
    [[OnlineAVplay shareAVPlayer] playWithUrl:[inTheRankSongsModel.auditionList[0] objectForKey:@"url"]];
    [OnlineAVplay shareAVPlayer].isPlay = YES;
    [[OnlineAVplay shareAVPlayer] play];
    
    [self presentViewController:[XCPlayViewController sharePlayViewController] animated:YES completion:^{
        
    }];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
- (void)leftBarButtonItemAction {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
